package com.fzhucc.modules.lesson.rest;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.modules.lesson.domain.dto.LessonQueryCriteria;
import com.fzhucc.modules.lesson.domain.dto.LessonTypeQueryCriteria;
import com.fzhucc.modules.lesson.domain.dto.LessonWithChapterDto;
import com.fzhucc.modules.lesson.domain.dto.LessonWxQueryCriteria;
import com.fzhucc.modules.lesson.service.LessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yyg
* @date 2022-12-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程:管理")
@RequestMapping("/api/lesson")
public class LessonController {

    private final LessonService lessonService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('lesson:list')")
    public void exportLesson(HttpServletResponse response, LessonQueryCriteria criteria) throws IOException {
        lessonService.download(lessonService.queryAll(criteria), response);
    }


    @Log("查询lesson")
    @ApiOperation("查询lesson")
    @AnonymousGetMapping("/show")
    /*@PreAuthorize("@el.check('lesson:list')")*/
    public ResponseEntity<Object> queryLesson(LessonQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(lessonService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("管理端查询lesson")
    @ApiOperation("管理端查询lesson")
    @GetMapping("/showIndex")
    /*@PreAuthorize("@el.check('lesson:list')")*/
    public ResponseEntity<Object> showIndex(LessonQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(lessonService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /*@PostMapping
    @Log("新增lesson")
    @ApiOperation("新增lesson")
    @PreAuthorize("@el.check('lesson:add')")
    public ResponseEntity<Object> createLesson(@Validated @RequestBody Lesson resources){
        return new ResponseEntity<>(lessonService.create(resources),HttpStatus.CREATED);
    }*/

    @PostMapping(value = "/createLessonWithChapter")
    @Log("新增课程包含一二级分类")
    @ApiOperation("新增课程包含一二级分类")
    @PreAuthorize("@el.check('lesson:add')")
    public ResponseEntity<Object> createLessonWithChapter(@Validated @RequestBody LessonWithChapterDto resources){
        return new ResponseEntity<>(lessonService.createLessonWithChapter(resources),HttpStatus.CREATED);
    }

    @PostMapping(value = "/updateLessonWithChapter")
    @Log("编辑课程包含一二级分类")
    @ApiOperation("编辑课程包含一二级分类")
    @PreAuthorize("@el.check('lesson:edit')")
    public ResponseEntity<Object> updateLessonWithChapter(@Validated @RequestBody LessonWithChapterDto resources){
        lessonService.editLessonWithChapter(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /*@PutMapping
    @Log("修改lesson")
    @ApiOperation("修改lesson")
    @PreAuthorize("@el.check('lesson:edit')")
    public ResponseEntity<Object> updateLesson(@Validated @RequestBody Lesson resources){
        lessonService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }*/

    @DeleteMapping
    @Log("删除lesson")
    @ApiOperation("删除lesson")
    @PreAuthorize("@el.check('lesson:del')")
    public ResponseEntity<Object> deleteLesson(@RequestBody Integer[] ids) {
        lessonService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/buyLesson")
    @Log("登录用户积分购买课程 购买成功则可阅读课程内容")
    @ApiOperation("登录用户积分购买课程 购买成功则可阅读课程内容")
    public ResponseEntity<Object> buyLesson(@RequestParam Integer lesson_id){
        return new ResponseEntity<>(lessonService.buyLesson(lesson_id),HttpStatus.OK);
    }

    @PostMapping(value = "/getLessonRecord")
    @Log("我的购买课程")
    @ApiOperation("我的购买课程")
    public ResponseEntity<Object> getLessonRecord(){
        return new ResponseEntity<>(lessonService.getLessonRecord(),HttpStatus.OK);
    }

    @PostMapping(value = "/getLessonRecordPage")
    @Log("我的购买课程分页")
    @ApiOperation("我的购买课程分页")
    public ResponseEntity<Object> getLessonRecordPage(Pageable pageable){
        return new ResponseEntity<>(lessonService.getLessonRecordPage(pageable),HttpStatus.OK);
    }


    @Log("判断当前用户是否购买过单个课程  购买则允许其观看视频/文章")
    @ApiOperation("判断当前用户是否购买过单个课程  购买则允许其观看视频/文章")
    @GetMapping(value = "/isBuy")
    public ResponseEntity<Object> isBuy(@RequestParam Integer lesson_id){
        return new ResponseEntity<>(lessonService.isBuy(lesson_id),HttpStatus.OK);
    }


    @Log("微信小程序查询课程列表 若有上下架时间 则根据上下架时间规则查询课程列表 如果有登录并判断是否学习过")
    @ApiOperation("微信小程序查询课程列表 若有上下架时间 则根据上下架时间规则查询课程列表 如果有登录并判断是否学习过")
    @AnonymousGetMapping("/wxShow")
    public ResponseEntity<Object> wxShow(LessonWxQueryCriteria criteria, Pageable pageable){
        //return new ResponseEntity<>(lessonService.wxShow(criteria,pageable),HttpStatus.OK);
        return new ResponseEntity<>(lessonService.wxShowByOrgArea(criteria,pageable),HttpStatus.OK);
    }

    @Log("微信小程序查询课程列表 若有上下架时间 则根据上下架时间规则查询课程列表 如果有登录并判断是否学习过")
    @ApiOperation("微信小程序查询课程列表 若有上下架时间 则根据上下架时间规则查询课程列表 如果有登录并判断是否学习过")
    @AnonymousGetMapping("/wxShowByOrgArea")
    public ResponseEntity<Object> wxShowByOrgArea(LessonWxQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(lessonService.wxShowByOrgArea(criteria,pageable),HttpStatus.OK);
    }


    @GetMapping(value = "/getLessonType")
    @Log("获取课程分类")
    @ApiOperation("获取课程分类")
    @AnonymousAccess
    public ResponseEntity<Object> getLessonType() {
        return new ResponseEntity<>(lessonService.findAllType(),HttpStatus.OK);
    }

    @GetMapping(value = "/getLessonTypeByOrgArea")
    @Log("获取课程分类ByOrgArea")
    @ApiOperation("获取课程分类ByOrgArea")
    public ResponseEntity<Object> getLessonType(LessonTypeQueryCriteria lessonTypeQueryCriteria) {
        return new ResponseEntity<>(lessonService.findAllTypeByOrgArea(lessonTypeQueryCriteria),HttpStatus.OK);
    }

}